package org.anyin.gitee.cloud.center.base.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.anyin.gitee.cloud.center.base.controller.form.SysRoutePageForm;
import org.anyin.gitee.cloud.center.base.convert.SysRouteConvert;
import org.anyin.gitee.cloud.center.base.controller.form.SysRouteSaveForm;
import org.anyin.gitee.cloud.center.base.repository.SysRouteRepository;
import org.anyin.gitee.cloud.center.base.service.SysRouteService;
import org.anyin.gitee.cloud.center.base.service.dto.SysRouteDTO;
import org.anyin.gitee.cloud.center.base.service.dto.SysRoutePageQueryDTO;
import org.anyin.gitee.cloud.lib.core.web.ApiResponse;
import org.anyin.gitee.cloud.lib.core.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/sys-route")
public class SysRouteController extends BaseController {

    @Autowired
    private SysRouteService sysRouteService;
    @Autowired
    private SysRouteConvert sysRouteConvert;

    @PostMapping("/create")
    public ApiResponse create(@Valid  @RequestBody SysRouteSaveForm form){
        SysRouteDTO route = sysRouteConvert.getSysRouteDTOByForm(form);
        sysRouteService.create(route);
        return ApiResponse.success();
    }

    @PostMapping("/modify/{id}")
    public ApiResponse modify(@PathVariable("id") Long id, @Valid  @RequestBody SysRouteSaveForm form){
        SysRouteDTO route = sysRouteConvert.getSysRouteDTOByForm(form);
        sysRouteService.modify(id, route);
        return ApiResponse.success();
    }

    @GetMapping("/info/{id}")
    public ApiResponse info(@PathVariable("id") Long id){
        SysRouteDTO route = sysRouteService.info(id);
        return ApiResponse.success(route);
    }

    @PostMapping("/delete/{id}")
    public ApiResponse delete(@PathVariable("id") Long id){
        sysRouteService.del(id);
        return ApiResponse.success();
    }

    @PostMapping("/page")
    public ApiResponse page(@Valid @RequestBody SysRoutePageForm form){
        SysRoutePageQueryDTO query = sysRouteConvert.getSysRoutePageQueryDTO(form);
        Page<SysRouteDTO> page = sysRouteService.page(query);
        return ApiResponse.success(page);
    }
}
